package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.crm.CrmFinanceInfo;
import com.nuoniu.sibanyun.mapper.crm.CrmFinanceInfoMapper;
import com.nuoniu.sibanyun.service.crm.ICrmFinanceInfoService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 财务信息 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@Service
public class CrmFinanceInfoServiceImpl extends ServiceImpl<CrmFinanceInfoMapper, CrmFinanceInfo> implements ICrmFinanceInfoService {

    @Autowired
    private CrmFinanceInfoMapper financeInfoMapper;

    @Override
    public Page<CrmFinanceInfo> getFinanceInfoList(Page page, CrmFinanceInfo financeInfo) {
        UserInfo userInfo = UserThreadLocal.get();
        return financeInfoMapper.getFinanceInfoList(page, financeInfo, userInfo);
    }

    @Override
    public CrmFinanceInfo getByCrmId(Integer crmId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("crm_id", crmId);
        queryWrapper.eq("status", 0);
        queryWrapper.last("LIMIT 1");
        CrmFinanceInfo one = getOne(queryWrapper);
        return one;
    }

    @Override
    public Boolean disableSelected(List<Long> ids) {
        if (null == ids || ids.size() == 0){
            throw new NuoNiuException("请选择要停用的记录");
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id",ids);
        updateWrapper.set("status",2);
        update(updateWrapper);
        return true;
    }

    @Override
    public Boolean enableSelected(List<Long> ids) {
        if (null == ids || ids.size() == 0){
            throw new NuoNiuException("请选择要启用的记录");
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id",ids);
        updateWrapper.set("status",1);
        update(updateWrapper);
        return true;
    }

}
